package cn.colins.consumer.filter;

import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.Cluster;
import org.apache.dubbo.rpc.cluster.Directory;
import org.apache.dubbo.rpc.cluster.LoadBalance;
import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @Description
 * @Author czl
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2023/5/9
 */
public class MyCluster implements Cluster {

    private final static Logger log= LoggerFactory.getLogger(MyCluster.class);

    @Override
    public <T> Invoker<T> join(Directory<T> directory) throws RpcException {
        return new AbstractClusterInvoker<T>(directory) {
            // Invocation 调用方法相关信息
            // invokers 服务提供者集合
            // loadbalance 负载均衡策略
            public Result doInvoke(Invocation invocation, List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {

                log.info("这是我自定义的集群策略");
                // 只是测试无脑选第一个
                return invokers.get(0).invoke(invocation);
            }
        };
    }

}
